package com.guigu.www.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.www.mapper.CellMapper;
import com.guigu.www.mapper.PayDetailsMapper;
import com.guigu.www.mapper.PayMapper;
import com.guigu.www.pojo.Pay;
import com.guigu.www.pojo.PayDetails;
import com.guigu.www.service.PayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements PayService {
    //获取map
    @Autowired
    PayMapper payMapper;

    @Autowired
    CellMapper cellMapper;

    @Autowired
    PayDetailsMapper payDetailsMapper;
    @Override
    public IPage<Pay> queryByPayCheckTag(int pageno, int pagesize) {
        //创建page
        Page page=new Page(pageno,pagesize);
        //获取条件
        QueryWrapper<Pay> queryWrapper=new QueryWrapper<Pay>();
        queryWrapper.eq("CHECK_TAG","S001-0");
        return this.page(page,queryWrapper);
    }

    @Override
    //查询当前出库单查询
    public Pay queryByPayPayId(String payId) {
        return payMapper.queryByPayPayId(payId);
    }
    //审核出库表数据是否通过
    @Override
    public boolean updatePayByChecktag(Pay pay) {
        return payMapper.updatePayByChecktag(pay)>0?true:false;
    }
    //查询所以出库申请
    @Override
    public IPage<Pay> queryByPayAll(int pageno, int pagesize, Pay pay) {
        //创建pagesize
        Page page=new Page(pageno,pagesize);
        //设置方法
        QueryWrapper<Pay> queryWrapper=new QueryWrapper<Pay>();
        if(StringUtils.isNotBlank(pay.getReason())){
            queryWrapper.eq("reason",pay.getReason());
        }
        return this.page(page,queryWrapper);
    }

    ////根据当前出库的id查询详情
    @Override
    public Pay queryByPayPayIdson(String payId) {
        return payMapper.queryByPayPayIdson(payId);
    }
    //查询所以出库申请审核成功的出库产品
    @Override
    public IPage<Pay> queryByPayChecktagtg(int pageno, int pagesize) {
        //创建page
        Page page=new Page(pageno,pagesize);

        //获取query
        QueryWrapper<Pay> queryWrapper=new QueryWrapper<Pay>();
        //设置
        queryWrapper.eq("check_tag","S001-1");
        queryWrapper.eq("pay_tag","");
        return this.page(page,queryWrapper);
    }

    @Override
    // //调度修改当前出库的状态
    public boolean updatePayByPayId(Pay pay) {
        return payMapper.updatePayByPayId(pay)>0?true:false;
    }
    //查询所有调度完成的产品
    @Override
    public IPage<Pay> queryPayByChecktagAll(int pageno, int pagesize) {
        //创建page
        Page page=new Page(pageno,pagesize);
        //获取设置方法
        QueryWrapper<Pay> queryWrapper=new QueryWrapper<Pay>();
        queryWrapper.eq("pay_tag","K002-2");
        return this.page(page,queryWrapper);
    }

    //根据当前编号查询明细 库存
    @Override
    public Pay queryByPayCellPayDetails(String payId) {
        return payMapper.queryByPayCellPayDetails(payId);
    }
    //修改出库表状态
    @Override
    public boolean updatoutpeyPaydetailsBystatus(Pay pay) {
        //判断当前出库数量  和确定出库数量是否相同  相同修改已经登记 不相同还是调度状态
        pay.setPayTag("K002-3");
        return payMapper.updatoutpeyPaydetailsBystatus(pay);
    }

    @Override
    //查询所有等待审核的出库产品
    public IPage<Pay> queryByPaycheckpending(int pageno, int pagesize) {
        //创建page
        Page page=new Page(pageno,pagesize);
        //设置方法
        QueryWrapper<Pay> queryWrapper=new QueryWrapper<Pay>();
        queryWrapper.eq("PAY_TAG","K002-3");
        return this.page(page,queryWrapper);
    }
    ////修改当前库存 库存数量减少
    @Override
    public boolean updateCellstatusaumont(List<PayDetails> payDetails) {
        int i=0;
        int i1=0;
        boolean f=false;
        for (PayDetails payDetail : payDetails) {
            //修改库存表
            i+=cellMapper.updateCellstatusaumont(payDetail.getPaidAmount(),payDetail.getProductId());
            //修改明细表
            if(Double.parseDouble(payDetail.getAmount())==payDetail.getWarehousing()){
               //修改状态
                payDetail.setPayTag("K002-1");
                payDetail.setWarehousing(payDetail.getWarehousing()+Double.parseDouble(payDetail.getPaidAmount()));
                i1+=payDetailsMapper.updateCellstatusaumont(payDetail);
            }else {
                payDetail.setPayTag("K002-2");
                payDetail.setWarehousing(payDetail.getWarehousing()+Double.parseDouble(payDetail.getPaidAmount()));
                i1+=payDetailsMapper.updateCellstatusaumont(payDetail);
            }
            //修改确定出库数量为哦
            payDetail.setPaidAmount("0");
            payDetailsMapper.updatpaidamount(payDetail);

        }
        if(payDetails.size()==i && payDetails.size()==i1){
            f=true;
        }
        return f;
    }
    //修改出库表数据
    @Override
    public boolean updatePayPayDetailsCellstatus(Pay pay) {
        if(Double.parseDouble(pay.getAmountSum())==Double.parseDouble(pay.getPaidAmountSum())){
            pay.setPayTag("K002-1");
        }else {
            pay.setPayTag("K002-2");
        }
        return payMapper.updatePayPayDetailsCellstatus(pay)>0?true:false;
    }

    //查询所有调度的出库产品
    @Override
    public IPage<Pay> outPayqllallshow(int pageno, int pagesize) {
        //创建page
        Page page=new Page(pageno,pagesize);
        //获取设置条件
        QueryWrapper<Pay> queryWrapper=new QueryWrapper<Pay>();
        queryWrapper.in("pay_tag","K002-2","K002-1","K002-3");

        return this.page(page,queryWrapper);
    }
}
